package cn.signalpha.modules.com.service;

import cn.signalpha.modules.com.entity.UserOauth;
import cn.signalpha.modules.com.mapper.UserOauthMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

import static cn.signalpha.common.cache.CacheNames.EXPIRATION_TIME_7;
import static cn.signalpha.common.cache.CacheNames.USER_OAUTH_KEY;

/**
 * 服务类
 *
 * @author signalpha
 */
@Service
public class UserOauthService extends ServiceImpl<UserOauthMapper, UserOauth> {

	/**
	 * 根据openId和来源查询
	 */
	@Cacheable(cacheNames = USER_OAUTH_KEY + EXPIRATION_TIME_7, key = "#openId+':'+#source")
	public UserOauth getByOpenIdAndSource(String openId, String source){
		return baseMapper.selectOne(Wrappers.<UserOauth>lambdaQuery().eq(UserOauth::getOpenId, openId).eq(UserOauth::getSource, source));
	}
	@CacheEvict(cacheNames = USER_OAUTH_KEY, key = "#openId+':'+#source")
	public void removeCaCheByOpenIdAndSource(String openId, String source) {
	}

	/**
	 * 根据用户id和来源查询
	 */
	public UserOauth getByUserIdAndSource(Long userId, String source){
		return baseMapper.selectOne(Wrappers.<UserOauth>lambdaQuery().eq(UserOauth::getUserId, userId).eq(UserOauth::getSource, source));
	}

	/**
	 * 根据用户id和来源查询
	 */
	public UserOauth getByUserIdAndSourceAndAppId(Long userId, String source, String appId){
		return baseMapper.selectOne(Wrappers.<UserOauth>lambdaQuery().eq(UserOauth::getUserId, userId).eq(UserOauth::getSource, source).eq(UserOauth::getAppId, appId));
	}

	/**
	 * 根据unionId查询
	 */
	public List<UserOauth> listByUnionId(String unionId){
		return baseMapper.selectList(Wrappers.<UserOauth>lambdaQuery().eq(UserOauth::getUnionId, unionId));
	}

	/**
	 * 根据unionId查询
	 */
	public List<UserOauth> listByUserId(Long userId){
		return baseMapper.selectList(Wrappers.<UserOauth>lambdaQuery().eq(UserOauth::getUserId, userId));
	}

}

